package com.yxt.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.yxt.common.sys.CommConstant;
import com.yxt.common.sys.CommUtil;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.widget.ListWheelAdapter;
import com.yxt.common.widget.OnWheelChangedListener;
import com.yxt.common.widget.WheelView;
import com.yxt.model.Division;
import com.yxt.model.Model;
import com.yxt.model.Valley;
import com.yxt.service.DivisionService;
import com.yxt.service.ValleyService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;  
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SystemSettingActivity extends Activity {
	private DivisionService diviservice = new DivisionService(SystemSettingActivity.this);
	private ValleyService vallservice = new ValleyService(SystemSettingActivity.this);
	private List<Division> provinces;
	private List<Division> cities;
	private List<Division> county;
	private List<Valley> mainriver;
	private List<Valley> childriver;	
	private TextView lblTemplate;
	private TextView lblProvince;
	private TextView lblRiver;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.system_setting);   //软件activity的布局	
		ActivityStackControlUtil.add(this);
		initComponent();
	}
	
	private void initComponent(){
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		Button btnConfirm = (Button)findViewById(R.id.btnConfirm);
		btnConfirm.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		
		lblTemplate = (TextView)findViewById(R.id.lblTemplate);
		lblProvince = (TextView)findViewById(R.id.lblProvince);
		lblRiver = (TextView)findViewById(R.id.lblRiver);		
		lblTemplate.setText(CommConstant.Main.CURRENT_TEMPLATE_NAME);
		lblProvince.setText(CommConstant.Main.CURRENT_AREA_NAME);
		lblRiver.setText(CommConstant.Main.CURRENT_RIVER_NAME);
		final List<Model> templates = new ArrayList<Model>();
		for(Iterator<Entry<String,String>> iterator = CommConstant.Main.TEMPLATE_MAP.entrySet().iterator();iterator.hasNext();){
			Entry<String,String> entry = iterator.next();
			templates.add(new Model(entry.getValue(),entry.getKey()));
		}
		
		LinearLayout pnlTemplate = (LinearLayout)findViewById(R.id.p1);
		pnlTemplate.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				View view = View.inflate(SystemSettingActivity.this, R.layout.comm_template, null);
				final AlertDialog menuDialog = new AlertDialog.Builder(SystemSettingActivity.this).create();
				menuDialog.setView(view);
				menuDialog.show();

				Button btnConfirm = (Button)view.findViewById(R.id.btnConfirm);
				Button btnCancel = (Button)view.findViewById(R.id.btnCancel);
				final WheelView wheelTemplate = (WheelView)view.findViewById(R.id.wheelTemplate);
				wheelTemplate.setAdapter(new ListWheelAdapter<Model>(templates));
				wheelTemplate.setCurrentItem(CommUtil.getCurrentTemplateIndex(templates,CommConstant.Main.CURRENT_TEMPLATE_CODE));
				btnConfirm.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						Model model = templates.get(wheelTemplate.getCurrentItem());
						lblTemplate.setText(model.getName());
						CommConstant.Main.CURRENT_TEMPLATE_CODE = model.getCode();
						CommConstant.Main.CURRENT_TEMPLATE_NAME = model.getName();
						CommUtil.persistenceTemplateInfo(getApplicationContext());
						menuDialog.cancel();
					}			
				});
				btnCancel.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						menuDialog.cancel();
					}			
				});
			}			
		});
		LinearLayout pnlArea = (LinearLayout)findViewById(R.id.l1);
		pnlArea.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				View view = View.inflate(SystemSettingActivity.this, R.layout.comm_region, null);
				final AlertDialog menuDialog = new AlertDialog.Builder(SystemSettingActivity.this).create();
				menuDialog.setView(view);
				menuDialog.show();
				final WheelView wheelProvince = (WheelView)view.findViewById(R.id.wheelProvince);
				final WheelView wheelCity = (WheelView)view.findViewById(R.id.wheelCity);
				final WheelView wheelCounty = (WheelView)view.findViewById(R.id.wheelCounty);
				String area = CommUtil.getDefaultAreaSql();		
				//获取区域列表
				provinces = diviservice.getDivisionByConditions(area);
				int currprovince = CommUtil.getCurrentRegionIndex(provinces, CommConstant.Main.CURRENT_AREA_CODE, CommConstant.Main.DEFAULT_PROVINCE_TYPE);
				cities = diviservice.getDivisionByConditions("type = '"+CommConstant.Rain.CITY_TYPE+"' and parentCode='" + provinces.get(currprovince).getCode()+"'");
				int currcity = CommUtil.getCurrentRegionIndex(cities, CommConstant.Main.CURRENT_AREA_CODE, CommConstant.Main.DEFAULT_CITY_TYPE);
				county = diviservice.getDivisionByConditions("type = '"+CommConstant.Rain.COUNTY_TYPE+"' and parentCode='" + cities.get(currcity).getCode()+"'");
				int currcounty = CommUtil.getCurrentRegionIndex(county, CommConstant.Main.CURRENT_AREA_CODE, CommConstant.Main.DEFAULT_COUNTY_TYPE);
				//设置当前项
				wheelProvince.setAdapter(new ListWheelAdapter<Division>(provinces,CommConstant.Config.REGION_MAXLENGTH));
				wheelCity.setAdapter(new ListWheelAdapter<Division>(cities,CommConstant.Config.REGION_MAXLENGTH));
				wheelCounty.setAdapter(new ListWheelAdapter<Division>(county,CommConstant.Config.REGION_MAXLENGTH));
				wheelProvince.setCurrentItem(currprovince);
				wheelCity.setCurrentItem(currcity);
				wheelCounty.setCurrentItem(currcounty);
				//添加事件
				wheelProvince.addChangingListener(new OnWheelChangedListener(){
					public void onChanged(WheelView wheel, int oldValue, int newValue) {
						cities = diviservice.getDivisionByConditions("type = '"+CommConstant.Rain.CITY_TYPE+"' and parentCode = '"+provinces.get(wheelProvince.getCurrentItem()).getCode()+"'");
						wheelCity.setAdapter(new ListWheelAdapter<Division>(cities,CommConstant.Config.REGION_MAXLENGTH));
					}
				});
				wheelCity.addChangingListener(new OnWheelChangedListener(){
					public void onChanged(WheelView wheel, int oldValue, int newValue) {
						county = diviservice.getDivisionByConditions("type = '"+CommConstant.Rain.COUNTY_TYPE+"' and parentCode = '"+cities.get(wheelCity.getCurrentItem()).getCode()+"'");	
						wheelCounty.setAdapter(new ListWheelAdapter<Division>(county,CommConstant.Config.REGION_MAXLENGTH));			
					}
				});
				//按钮点击事件
				Button btnConfirm = (Button)view.findViewById(R.id.btnConfirm);
				Button btnCancel = (Button)view.findViewById(R.id.btnCancel);
				btnConfirm.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						Division proDivision = provinces.get(wheelProvince.getCurrentItem());
						Division cityDivision = cities.get(wheelCity.getCurrentItem());
						Division countyDivision = county.get(wheelCounty.getCurrentItem());
						if (!CommConstant.Config.DEFAULT_NAME.equals(countyDivision.getName())) {
							CommConstant.Main.CURRENT_AREA_CODE = countyDivision.getCode();
							CommConstant.Main.CURRENT_AREA_NAME = countyDivision.getName();
							lblProvince.setText(countyDivision.getName());
						} else {
							if (!CommConstant.Config.DEFAULT_NAME.equals(cityDivision.getName())) {
								CommConstant.Main.CURRENT_AREA_CODE = cityDivision.getCode();
								CommConstant.Main.CURRENT_AREA_NAME = cityDivision.getName();
								lblProvince.setText(cityDivision.getName());
							} else {
								CommConstant.Main.CURRENT_AREA_CODE = proDivision.getCode();
								CommConstant.Main.CURRENT_AREA_NAME = proDivision.getName();
								lblProvince.setText(proDivision.getName());
							}
						}
						menuDialog.cancel();
						CommUtil.persistenceAreaInfo(getApplicationContext());
					}			
				});
				btnCancel.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						menuDialog.cancel();
					}			
				});
			}			
		});
		LinearLayout pnlRiver = (LinearLayout)findViewById(R.id.l2);
		pnlRiver.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				View view = View.inflate(SystemSettingActivity.this, R.layout.comm_river, null);
				final AlertDialog menuDialog = new AlertDialog.Builder(SystemSettingActivity.this).create();
				menuDialog.setView(view);
				menuDialog.show();
				
				final WheelView wheelRiver = (WheelView)view.findViewById(R.id.wheelRiver);
				final WheelView wheelSubRiver = (WheelView)view.findViewById(R.id.wheelSubRiver);
				String mriver = CommUtil.getDefaultRiverSql(CommConstant.Water.DEFAULT_MAIN_RIVER);
				if (mriver.indexOf("type") == -1) {
					mriver += " and type = '"+CommConstant.Water.MAIN_TYPE+"'";
				}
				mainriver = vallservice.getValleyByCondtion(mriver);
				String sriver = CommUtil.getDefaultRiverSql(CommConstant.Water.DEFAULT_SUB_RIVER);
				int currmain = CommUtil.getCurrentRiverIndex(mainriver, CommConstant.Main.CURRENT_RIVER_CODE, CommConstant.Main.DEFAULT_MAIN_TYPE);
				if (sriver.indexOf("type") > -1) {//判断是否为空
					sriver += " and parentCode = '"+mainriver.get(currmain).getCode()+"'";
				} else {
					sriver += " and type = '"+CommConstant.Water.SUB_TYPE+"' and parentCode = '"+mainriver.get(currmain).getCode()+"'";
				}
				childriver = vallservice.getValleyByCondtion(sriver);
				int currsub = CommUtil.getCurrentRiverIndex(childriver, CommConstant.Main.CURRENT_RIVER_CODE, CommConstant.Main.DEFAULT_SUB_TYPE);
				wheelRiver.setAdapter(new ListWheelAdapter<Valley>(mainriver,CommConstant.Config.RIVER_MAXLENGTH));
				wheelSubRiver.setAdapter(new ListWheelAdapter<Valley>(childriver,CommConstant.Config.RIVER_MAXLENGTH));
				wheelRiver.setCurrentItem(currmain);
				wheelSubRiver.setCurrentItem(currsub);
				//添加事件
				wheelRiver.addChangingListener(new OnWheelChangedListener(){
					public void onChanged(WheelView wheel, int oldValue, int newValue) {
						childriver = vallservice.getValleyByCondtion("type = '"+CommConstant.Water.SUB_TYPE+"' and parentCode = '"+mainriver.get(wheelRiver.getCurrentItem()).getCode()+"'");
						wheelSubRiver.setAdapter(new ListWheelAdapter<Valley>(childriver,CommConstant.Config.RIVER_MAXLENGTH));
					}
				});
				
				Button btnConfirm = (Button)view.findViewById(R.id.btnConfirm);
				Button btnCancel = (Button)view.findViewById(R.id.btnCancel);
				btnConfirm.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						Valley subValley = childriver.get(wheelSubRiver.getCurrentItem());
						Valley mainValley = mainriver.get(wheelRiver.getCurrentItem());
						if (!CommConstant.Config.DEFAULT_NAME.equals(subValley.getName())) {
							CommConstant.Main.CURRENT_RIVER_CODE = subValley.getCode();
							CommConstant.Main.CURRENT_RIVER_NAME = subValley.getName();
							lblRiver.setText(subValley.getName());
						} else {
							CommConstant.Main.CURRENT_RIVER_CODE = mainValley.getCode();
							CommConstant.Main.CURRENT_RIVER_NAME = mainValley.getName();
							lblRiver.setText(mainValley.getName());
						}
						CommUtil.persistenceRiverInfo(getApplicationContext());
						menuDialog.cancel();
					}			
				});
				btnCancel.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
						menuDialog.cancel();
					}			
				});
			}			
		});
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {  
		if(keyCode == KeyEvent.KEYCODE_BACK){
			return false;
		}
		return super.onKeyDown(keyCode, event); 
	}

	protected void onDestroy() {
		super.onDestroy();
		diviservice.closeDB();
		vallservice.closeDB();
        ActivityStackControlUtil.remove(this);
	}
}